package com.sena.crawler.util;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * url tool
 *
 * @author seven-10-10 14:57:05
 */
public class UrlUtil {

    /**
     * url格式校验
     */
    public static boolean isUrl(String url) {
        if (url != null && url.trim().length() > 0 && url.startsWith("http") && !url.endsWith("css") && !url.endsWith("js")) {
            return true;
        }
        return false;
    }



    /**
     * 本站Url校验
     *
     * @param url
     * @param baseUrl
     * @return
     */
    public static String cleanInUrl(String url, String baseUrl) {
        if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(baseUrl) && !url.endsWith("js") &&
                !url.endsWith("css") && !url.endsWith("js#")) {
            //判断是本站的连接
            if (url.contains("./") || url.contains("../")) {
                try {
                    URL absoluteUrl = new URL(baseUrl);
                    URL parseUrl = new URL(absoluteUrl, url);
                    return parseUrl.toString();
                } catch (Exception e) {
                    return null;
                }
            }
        } else {
            return url;
        }
        return null;
    }

    /**
     * 检查url可以连接
     *
     * @param URL
     * @return
     */
    public static boolean checkUrl(String URL) {
        try {
            URL url = new java.net.URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0");
            connection.connect();
            connection.setConnectTimeout(1000);
            connection.setInstanceFollowRedirects(true);
            connection.setConnectTimeout(500);
            if (connection.getResponseCode() != 404 && connection.getResponseCode() != 500) {//判断状态码
                String htmlCode = "";
                InputStream in = connection.getInputStream();
                byte[] buffer = new byte[500];
                in.read(buffer);
                htmlCode = new String(buffer);
                if (htmlCode.contains("错误页") || htmlCode.contains("{\"message\":\"请求的URI地")) {//拿到返回内容判断是否返回的是错误页
                    return false;
                }
            } else {
                return false;
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

}
